<?php
session_start();
require_once('system/config/main_config.php'); // Подключаем основной файл конфигурации

$getStrPos = strpos($_SERVER['REQUEST_URI'], '?');
$strUrl = substr($_SERVER['REQUEST_URI'], 0, $getStrPos ? $getStrPos : strlen($_SERVER['REQUEST_URI']));
$arrRequest = explode('/', substr($strUrl, 1)); // Преобразовываем URL к массиву

for ($i = 0; $i <= 5; $i++) {
	if (!isset($arrRequest[$i])) $arrRequest[$i] = ''; // Для упрощения обработки контроллером
}

//if (City::getSub()) {
		
	// Определение контроллера для обработки запроса
	switch ($arrRequest[0]) {
		case '': $controller = new Controller_Index($arrRequest); break;
		case 'city': $controller = new Controller_City($arrRequest); break;
		case 'parse': $controller = new Controller_Parse($arrRequest); break;
		case 'admin':
			switch ($arrRequest[1]) {
				case '': $controller = new Controller_Admin_Base($arrRequest); break;
				case 'city': $controller = new Controller_Admin_City($arrRequest); break;
				case 'country': $controller = new Controller_Admin_Country($arrRequest); break;
				case 'resort': $controller = new Controller_Admin_Resort($arrRequest); break;
				default: $error = 404; break;
			}
			break;
		case 'tour': {
			$objLvlTour = new Lvl_Tour(intval($arrRequest[1]));
			if ($objLvlTour->id) {
				header('location: http://level.travel' . $objLvlTour->getField('link'));
				exit();
			}
			$error = 404;
			break;
		}
		case 'ajax':
			switch ($arrRequest[1]) {
				case 'photo': $controller = new Controller_Ajax_Photo($arrRequest); break;
				default: $error = 404; break;
			}
			break;
		default: $controller = new Controller_Default($arrRequest);
	}
	if (City::getSub()) {
		$objCity = City::getCity();
		if (!$objCity->id) $error = 404;
	}
	elseif (empty($arrRequest[0])) $controller = new Controller_Main($arrRequest);
//}



try {
	if (isset($error)) throw new Exception_SiteError('', $error);
	$controller->action();
}
catch (Exception_SiteError $e) {
	$errors = array(404, 403, 503);
	if (in_array($e->getCode(), $errors)) {
		switch ($e->getCode()) {
			case 404:
				header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
				break;
			case 403:
				header($_SERVER["SERVER_PROTOCOL"]." 403 Forbidden");
				break;
			case 503:
				header($_SERVER["SERVER_PROTOCOL"]." 503 Service Unavailable");
				break;
		}
		echo '<h1>' . $e->getCode() . '</h1>';
	}
	exit();
}